//
//  BillboardCollectionViewCell.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "BillboardCollectionViewCell.h"
#import "FitHeader.h"
#import "BillboardTableViewCell.h"
#import "TwoToOne.h"
#import "Billboard.h"
#import "UIImageView+WebCache.h"
#import "HeadView.h"
#import "UIImage+GIF.h"
//#import "LZHeadViewController.h"
#import "LZBillboardViewController.h"
#import "Model.h"

@implementation BillboardCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        _array = @[@"节目榜单", @"主播榜单"];
        _dataSourceArray = [NSMutableArray array];
        _bigDataSourceArray = [NSMutableArray array];
        _billBoardTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.height) style:UITableViewStylePlain];
        _billBoardTableView.dataSource = self;
        _billBoardTableView.delegate = self;
        [self.contentView addSubview:_billBoardTableView];
        
        [self getData];
        
        
        _headImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 180 * FITHeight)];
        
        _billBoardTableView.tableHeaderView = _headImageView;
        
        self.progressView = [[MBProgressHUD alloc]initWithView:self.contentView];
//        _progressView.labelText = @"努力加载中...";
        
        [[[_progressView subviews]firstObject] removeFromSuperview];
        [[[_progressView subviews] objectAtIndex:1] removeFromSuperview];
        [_progressView setMode:0];
        [_progressView show:YES];
        _progressView.color = [UIColor clearColor];
        NSString  *name = @"RED.gif";
        NSString  *filePath = [[NSBundle bundleWithPath:[[NSBundle mainBundle] bundlePath]] pathForResource:name ofType:nil];
        NSData  *imageData = [NSData dataWithContentsOfFile:filePath];
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(-70 * FITWidth, -80 * FITHeight, 140 * FITWidth, 100 * FITHeight)];
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 10;
        imageView.opaque = NO;
        
        [[[[[_progressView subviews] lastObject] subviews] firstObject] removeFromSuperview];

        imageView.image = [UIImage sd_animatedGIFWithData:imageData];
        [[[_progressView subviews] lastObject] addSubview:imageView];
        
        [self.contentView addSubview:_progressView];
        [self changeColor];
    }
    return self;
}


-(void)changeColor{
    self.backgroundColor = [Model shareInstance].ViewColor;
}

- (void)getData{
    NSString *urlString = @"http://mobile.ximalaya.com/mobile/discovery/v2/rankingList/group?channel=and-c57&device=android&includeActivity=true&includeSpecial=true&scale=2&version=4.3.20.2";
    [TwoToOne getDataByString:urlString BodyString:nil WithDataBlock:^(id data) {
        NSDictionary *Dic = [data objectForKey:@"focusImages"];
        NSArray *arrayList = [Dic objectForKey:@"list"];
        self.dicList = arrayList.firstObject;
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:[_dicList objectForKey:@"pic"]]];
        NSArray *array = [data objectForKey:@"datas"];
        NSDictionary *dic = [array firstObject];
        NSArray *array1 = [dic objectForKey:@"list"];
        for (NSDictionary *Dic in array1) {
            Billboard *bill = [[Billboard alloc]init];
            [bill setValuesForKeysWithDictionary:Dic];
            [_dataSourceArray addObject:bill];
        }
        
        NSDictionary *dicTwo = [array lastObject];
        NSArray *arrayTwo = [dicTwo objectForKey:@"list"];
        for (NSDictionary *Dic in arrayTwo) {
            Billboard *bill = [[Billboard alloc]init];
            [bill setValuesForKeysWithDictionary:Dic];
            [_bigDataSourceArray addObject:bill];
        }
        
        [_billBoardTableView reloadData];
        [_progressView removeFromSuperview];
    }];
}
//#pragma mark - 上方图片的轻拍时间
//- (void)tapAction{
//    LZHeadViewController *headvc = [[LZHeadViewController alloc]init];
//    headvc.longTitle = [_dicList objectForKey:@"longTitle"];
//    [self.nav pushViewController:headvc animated:YES];
//}
#pragma mark - cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 120 * FITHeight;
}
#pragma mark - cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataSourceArray.count;
}
#pragma mark - section的头部
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return _array[section];
}
#pragma mark - section的个数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _array.count;
}
#pragma mark - cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"cell";
    BillboardTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[BillboardTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.selectionStyle = 0;
    [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
    if (indexPath.section == 0) {
        Billboard *billBoard = _dataSourceArray[indexPath.row];
        cell.billBoard = billBoard;
    }else{
        Billboard *billBoard1 = _bigDataSourceArray[indexPath.row];
        cell.billBoard1 = billBoard1;
    }
    return cell;
}
#pragma mark - 点击跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LZBillboardViewController *lz = [[LZBillboardViewController alloc]init];
    if (indexPath.section == 0) {
        Billboard *billBoard = _dataSourceArray[indexPath.row];
        lz.bill = billBoard;
    }else{
        Billboard *billBoard1 = _bigDataSourceArray[indexPath.row];
        lz.bill = billBoard1;
    }
    lz.index = indexPath.row;
    lz.section = indexPath.section;
    [self.nav pushViewController:lz animated:YES];
}
@end
